// pages/mine/mine.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isGetUserInfoDialog:false,

    userImg:'/images/header_default.jpg',
    userName:'未登录用户',
  
    allLottery:0,
    lotterying:0,
    lucky:0,

    is_owner:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkUserInfo();

    this.getUserLottery();
    
    // this.getUserLottery();
  },

  onShow: function(){
    this.checkUserInfo();

    this.setData({
      is_owner : app.globalData.acc.is_owner
    })

    this.getUserLottery();
    
  },

  // 检测是否获取个人信息
  checkUserInfo:function(){
    var that = this;
    var acc = app.globalData.acc;
    // console.log(app.globalData.acc);
    // console.log(this.data.isGetUserInfoDialog);
    
    
    if(acc.nick_name != null && acc.nick_name != ""){
      that.setData({
        userImg : acc.avatar_url,
        userName : acc.nick_name
      })
    }else{
      that.setData({
        isGetUserInfoDialog : true
      })
    }
  },

  // 获取用户的所有抽奖信息
  getUserLottery:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + 'lotteryaccs/get_acc_lottery_info',
      data: {
        uid:app.globalData.acc.id
      },
      method: 'POST',
      success: (res)=>{
        console.log("******************");
        
        console.log(res);
        
        that.setData({
          allLottery:res.data.data.all,
          lotterying:res.data.data.lotterying,
          lucky:res.data.data.lucky,
        })
      },
      fail: ()=>{

      }
    });
  },

  // 关闭提示授权界面
  closeLogin:function(){
    this.setData({
      isGetUserInfoDialog:false
    })
  },
  // 登录
  onGotUserInfo: function(e) {
    var that = this
    if(e.detail.errMsg == 'getUserInfo:ok'){
      // console.log(e.detail.userInfo)
      // userinfo存入全局 
      app.globalData.userInfo = e.detail.userInfo

      app.globalData.acc.nick_name = e.detail.userInfo.nickName
      app.globalData.acc.avatar_url = e.detail.userInfo.avatarUrl
      // 保存数据库
      wx.request({
        url: app.globalData.url + 'lotteryaccs/updateAccInfo',
        data: {
          openid:app.globalData.acc.openid,
          avatar_url:e.detail.userInfo.avatarUrl,
          nick_name:e.detail.userInfo.nickName
        },
        method: 'POST',
        success: (res)=>{
          // console.log("***********")
          // console.log(res)
          if(res.data.msg == 1){
            console.log("信息已存入");
            that.closeLogin();
            // that.checkUserInfo();
            that.setData({
              userImg:e.detail.userInfo.avatarUrl,
              userName:e.detail.userInfo.nickName
            })
          }
        },
        fail: ()=>{
          // wx.showToast({
          //   title: '参与失败，请重试',
          //   icon: 'none',
          //   duration: 1500,
          // });
        },
      });
    }
    if(e.detail.errMsg == 'getUserInfo:fail auth deny'){
      // console.log('fail')
    }
  },
  
  //
  toIndex:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})